package com.htg.dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;


public class DialogYunChaxun {

    private Context cxt;

    private AlertDialog alertDialog = null;


    public DialogYunChaxun(Context cxt) {
        this.cxt = cxt;
    }

    public void show(OnOkClickListener onOkClickListener) {
        if (alertDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(cxt);
            alertDialog = builder.create();
        } else {
            if (alertDialog.isShowing()) {
                return;
            }
        }
        alertDialog.show();
        View view = LayoutInflater.from(cxt).inflate(R.layout.dialog_yunchaxun, null);
        Window window = alertDialog.getWindow();
        window.setBackgroundDrawableResource(R.drawable.shape_white_rounded_12dp);
        WindowManager.LayoutParams params = window.getAttributes();
        int width = cxt.getResources().getDimensionPixelSize(R.dimen.dp_280);

        params.width = width;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        //window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
        window.setContentView(view);

        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) CheckBox cbJichuchaxun = view.findViewById(R.id.cb_dialog_yunchaxun_jichuchaxun);
        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) CheckBox cbShixuyingxiang = view.findViewById(R.id.cb_dialog_yunchaxun_shixuyingxiang);
        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) TextView tvCancel = view.findViewById(R.id.tv_dialog_yunchaxun_cancel);
        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) TextView tvOk = view.findViewById(R.id.tv_dialog_yunchaxun_ok);


        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();

            }
        });
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!cbJichuchaxun.isChecked() && !cbShixuyingxiang.isChecked()) {
                    Toast.makeText(cxt,"至少选择一个类型",Toast.LENGTH_LONG);
                    return;
                }
                alertDialog.dismiss();
                List<Integer> types = new ArrayList<>();
                if (cbJichuchaxun.isChecked())
                    types.add(0);
                if (cbShixuyingxiang.isChecked())
                    types.add(1);
                if (onOkClickListener != null)
                    onOkClickListener.onYunChaxunOkClickListener(types);
            }
        });


    }


    public void dismis() {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
        alertDialog = null;
    }

    public interface OnOkClickListener {
        /**
         * @param types 0：基础查询，1：时序影响 2：高级查询 。。。
         */
        void onYunChaxunOkClickListener(List<Integer> types);
    }


}
